var app = getApp();
var url = app.url;
var util = require('../../utils/util');
var config = require('../../utils/config');
var db = require('../../utils/db');
var isbn13;
var qty;
var empno = 'FE717';//暂时hard code，应该是从登陆用户找到对应的工号

Page({
  data: {
    bookMsg: {},
    date : util.formatDate(new Date()),
    borrowBookdate:util.formatDate(new Date()),
    reuturnBookdate:util.formatDate(new Date()),

  },
  inputChange: function (e) {
    this.data.addBookQty = e.detail.value;
    //book["qty"] = e.detail.value;
    //that.bookMsg.qty=e.detail.value;
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数

    var that = this;

    that.setData({
        bookMsg:JSON.parse(options.book),
        reuturnBookdate:util.returnDate(this.data.borrowBookdate,3)
    });

  },
  // 开始时间
  bindDateChange:function(e){
    this.setData({
        borrowBookdate : e.detail.value,
        reuturnBookdate:util.returnDate(this.data.borrowBookdate,3)
    })
  },

  // 借阅时长
  slider3change:function(e){
    this.setData({
        reuturnBookdate:util.returnDate(this.data.borrowBookdate,e.detail.value)
    })
    // reuturnBookdate = 开始时间+时长

  },

  formSubmit:function(e){
    var formdata = e.detail.value
    formdata.isbn13 = this.data.bookMsg.isbn13
    formdata.openid = wx.getStorageSync('openid')

    console.log(formdata)

    // 发送数据到 web服务器,完成借阅
    util.request({
      url: 'http://192.168.81.130:5000/api/book/borrow',
      data:formdata,
      method:'POST'
    },function(res){
    
      // 判断返回的结果
      if(res.data.error == 0){
        wx.showToast({
          title:res.data.msg,
          icon: 'success',
          duration: 2000,
          complete:function(){
            // 跳转到已借阅的页面
            wx.navigateTo({
              url: '../bookList/bookList'
            });

          }
        })
      
      }else{
        wx.showToast({
          title:res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  onReady: function () {
    // 页面渲染完成

  },
  onShow: function () {
    // 页面显示

  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  }
  
})